/**
 * 文件名：****.java.
 * 版权：Copyright Lightkit All Rights Reserved.
 * 描述：text.
 */
package com.lightkits.wms.domain.orderscanner.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lightkits.common.db.entity.BdTag;
import com.lightkits.common.db.entity.WmSaleTicketLine;
import com.lightkits.common.db.entity.event.BdEvent;
import com.lightkits.framework.common.exception.LightKitsBizException;
import com.lightkits.framework.common.logging.Logger;
import com.lightkits.framework.common.logging.LoggerFactory;
import com.lightkits.framework.common.model.CheckResult;
import com.lightkits.framework.common.utility.date.DateUtil;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.wms.domain.orderscanner.CreateTagAndSubParams;
import com.lightkits.wms.dto.orderscanner.model.ScannerHeaderDto;
import com.lightkits.wms.dto.orderscanner.model.ScannerLineDto;
import com.lightkits.wms.dto.stocktransfer.model.SubStockViewDTO;
import com.lightkits.wms.dto.tagservice.model.TagViewDto;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author jing.wei@hand-china.com
 * @date 2019/6/20 17:55
 **/
@Service
public class CreateTagAndSubParamsImpl implements CreateTagAndSubParams {
    private final Logger LOGGER = LoggerFactory.getLogger(CreateTagAndSubParamsImpl.class);

    /**
     * 创建子库存转移参数
     *
     * @param headDTO  订单扫描请求头信息
     * @param lineDTOS 订单扫描请求行信息
     * @return SubStockViewDTO
     **/
    @Override
    public CheckResult<List<SubStockViewDTO>> createSubStockParam(ScannerHeaderDto headDTO,
                                                                  List<ScannerLineDto> lineDTOS) {
        CheckResult<List<SubStockViewDTO>> checkResult = new CheckResult<>();
        checkResult.setSuccessful(true);
        List<SubStockViewDTO> subStockViewDTOS = new ArrayList<>();
        try {
            for (ScannerLineDto line : lineDTOS) {
                SubStockViewDTO viewDTO = new SubStockViewDTO();
                viewDTO.setWorkerId(headDTO.getWorkerId());
                viewDTO.setOrgId(headDTO.getOrgId());
                viewDTO.setItemId(line.getItemId());
                viewDTO.setUomCode(line.getUomCode());
                viewDTO.setLot(line.getLotNum());
                viewDTO.setOutWarehouseId(line.getWarehouseId());
                viewDTO.setOutWmAreaId(line.getWmAreaId());
                viewDTO.setInWarehouseId(line.getToWarehouseId());
                viewDTO.setOwnerType("OWN");
                viewDTO.setOwnerId(line.getOwnerId());
                viewDTO.setTransferQty(line.getQuantity());
                viewDTO.setMoveTypeCode("WM_ALLOT");
                subStockViewDTOS.add(viewDTO);
            }
            checkResult.setData(subStockViewDTOS);
        } catch (Exception e) {
            LOGGER.error("Create SubStocker Params.error", e);
        }
        return checkResult;
    }

    /**
     * 创建条码信息
     *
     * @param lines           扫描订单行
     * @param saleTicketLines 售货单行
     * @param orgId           组织ID(转移的)
     * @return TagViewDto
     */
    @Override
    public CheckResult<List<TagViewDto>> createTags(List<ScannerLineDto> lines, BdEvent event, List<WmSaleTicketLine> saleTicketLines,
                                                    String orgId) {
        CheckResult<List<TagViewDto>> checkResult = new CheckResult<>();
        checkResult.setSuccessful(true);
        List<TagViewDto> tagViewDTOS = new ArrayList<>();
        try {
            for (ScannerLineDto dto : lines) {
                TagViewDto viewDTO = new TagViewDto();
                Optional<WmSaleTicketLine> opt = saleTicketLines.stream().filter(target -> target.getTagId().toString()
                        .equals(dto.getTagId())).findFirst();
                viewDTO.setTagCode(dto.getTagCode());
                BdTag tag = new BdTag().selectOne(new EntityWrapper().eq(BdTag.TAG_CODE, dto.getTagCode()));
                viewDTO.setTagId(tag.getId().toString());
                viewDTO.setTagType(tag.getTagType());
                viewDTO.setDocumentId(opt.get().getId().toString());
                viewDTO.setOwnerOrgId(orgId);
                viewDTO.setEventId(event.getId());
                viewDTO.setEventBy(event.getWorkerId());
                viewDTO.setEventTime(DateUtil.toDateString(event.getCreationDate()));
                viewDTO.setVerificationCode(tag.getVerificationCode());
                viewDTO.setDCode(tag.getDCode());
                viewDTO.setProductCode(tag.getProductCode());
                viewDTO.setWarehouseId(dto.getToWarehouseId());
                viewDTO.setStorageUnitCode(tag.getStorageUnitCode());
                viewDTO.setTagStatus(tag.getTagStatus());
                viewDTO.setContainerOwnerType(tag.getContainerOwnerType());
                viewDTO.setContainerType(tag.getContainerType());
                viewDTO.setRemark(tag.getRemark());
                if (!ObjectUtil.isEmpty(tag.getCapacityQty())) {
                    viewDTO.setCapacityQty(tag.getCapacityQty().stripTrailingZeros().toString());
                }
                if (!ObjectUtil.isEmpty(tag.getOuterTagId())) {
                    viewDTO.setOuterTagId(tag.getOuterTagId().toString());
                }
                if (!ObjectUtil.isEmpty(tag.getResourceId())) {
                    viewDTO.setResourceId(tag.getResourceId().toString());
                }
                if (!ObjectUtil.isEmpty(tag.getLocationId())) {
                    viewDTO.setLocationId(tag.getLocationId().toString());
                }
                if (!ObjectUtil.isEmpty(tag.getContainerId())) {
                    viewDTO.setContainerId(tag.getContainerId().toString());
                }
                if (!ObjectUtil.isEmpty(tag.getDocumentTypeId())) {
                    viewDTO.setDocumentTypeId(tag.getDocumentTypeId().toString());
                }
                if (!ObjectUtil.isEmpty(tag.getDocumentId())) {
                    viewDTO.setDocumentId(tag.getDocumentId().toString());
                }
                viewDTO.setItemId(dto.getItemId());
                viewDTO.setTagThingType("ITEM");
                viewDTO.setUomCode(dto.getUomCode());
                viewDTO.setPrimaryUomCode(dto.getUomCode());
                if (!ObjectUtil.isEmpty(dto.getQuantity())) {
                    viewDTO.setQuantity(dto.getQuantity());
                    viewDTO.setPrimaryQuantity(dto.getQuantity());
                }
                viewDTO.setLotNumber(dto.getLotNum());
                viewDTO.setOwnerType("OWN");
                if (!ObjectUtil.isEmpty(dto.getOwnerId())) {
                    viewDTO.setOwnerId(dto.getOwnerId());
                }
                tagViewDTOS.add(viewDTO);
            }
            checkResult.setData(tagViewDTOS);

        } catch (Exception e) {
            LOGGER.error("Create Tags.error", e);
        }
        return checkResult;
    }
}
